/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import javax.management.Query;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import pojo.HangSanXuat;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class HangSanXuatDAO {

    public static List<HangSanXuat> layDanhSachHangSanXuat() {
        List<HangSanXuat> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select hsx from HangSanXuat hsx";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static HangSanXuat layThongTinNhaSanXuat(int maHangSX) {
        HangSanXuat hsx = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            hsx = (HangSanXuat) session.get(HangSanXuat.class, maHangSX);
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return hsx;
    }

    public static void themHangSanXuat(HangSanXuat hsx) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(hsx);
        session.getTransaction().commit();
        session.close();
    }

    public static int getSoLuong() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select count(*) from HangSanXuat hsx";
        int count = ((Long) session.createQuery(hql).uniqueResult()).intValue();
        return count;
    }

    public static List<HangSanXuat> timKiem(String tenHang) {
        List<HangSanXuat> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select hsx from HangSanXuat hsx where hsx.tenHang like :tenHang";
            org.hibernate.Query query = session.createQuery(hql);
            query.setString("tenHang", "%" + tenHang + "%");
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static boolean xoaHangsx(int maHangsx) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        HangSanXuat hsx = HangSanXuatDAO.layThongTinNhaSanXuat(maHangsx);
        if (hsx == null) {
            return false;
        }
        session.beginTransaction();
        session.delete(hsx);
        session.getTransaction().commit();
        session.close();
        return true;
    }

    public static void capNhatThongTinHangsx(HangSanXuat hsx) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(hsx);
        session.getTransaction().commit();
        session.close();
    }
}
